package com.gitee.qdbp.general.mgr.controller.permission;

import java.util.ArrayList;
import java.util.List;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.gitee.qdbp.able.exception.ServiceException;
import com.gitee.qdbp.able.jdbc.ordering.OrderPaging;
import com.gitee.qdbp.able.jdbc.paging.PageList;
import com.gitee.qdbp.able.result.ResponseMessage;
import com.gitee.qdbp.base.system.model.IAccount;
import com.gitee.qdbp.base.system.service.IDefaultValueConfig;
import com.gitee.qdbp.base.utils.EnumTools;
import com.gitee.qdbp.base.utils.SessionTools;
import com.gitee.qdbp.general.system.api.permission.model.ResourceCoreBean;
import com.gitee.qdbp.general.system.api.permission.model.ResourceCoreWhere;
import com.gitee.qdbp.general.system.api.permission.model.RoleCoreBean;
import com.gitee.qdbp.general.system.api.permission.model.RoleCoreUpdate;
import com.gitee.qdbp.general.system.api.permission.model.RoleCoreWhere;
import com.gitee.qdbp.general.system.api.permission.model.RoleResourceRefBean;
import com.gitee.qdbp.general.system.api.permission.model.RoleResourceRefWhere;
import com.gitee.qdbp.general.system.api.permission.service.IResourceCoreExecutor;
import com.gitee.qdbp.general.system.api.permission.service.IResourceCoreQueryer;
import com.gitee.qdbp.general.system.api.permission.service.IRoleCoreExecutor;
import com.gitee.qdbp.general.system.api.permission.service.IRoleCoreQueryer;
import com.gitee.qdbp.tools.utils.ConvertTools;
import com.gitee.qdbp.tools.utils.VerifyTools;

/**
 * 角色基本信息控制器
 *
 * @author zhaohuihua
 * @version 170401
 */
@Controller
@RequestMapping("actions/role")
public class RoleController {

    @Autowired
    private IRoleCoreQueryer roleCoreQueryer;
    @Autowired
    private IRoleCoreExecutor roleCoreExecutor;
    @Autowired
    private IResourceCoreQueryer resourceCoreQueryer;
    @Autowired
    private IResourceCoreExecutor resourceCoreExecutor;
    @Autowired
    private IDefaultValueConfig defaultValues;

    /**
     * 查询资源列表
     *
     * @param paging 分页信息
     * @param model 查询条件
     * @return 资源列表
     */
    @ResponseBody
    @RequestMapping("resource/list")
    @RequiresPermissions("permission:role:query")
    public ResponseMessage list(ResourceCoreWhere model, OrderPaging paging, Boolean extra) {
        try {
            PageList<ResourceCoreBean> list = resourceCoreQueryer.list(model, paging);
            ResponseMessage result = new ResponseMessage(list);
            if (Boolean.TRUE.equals(extra)) {
                Class<? extends Enum<?>> userType = defaultValues.getUserTypeClass();
                result.addExtra("UserType", EnumTools.getEnumResource(userType));
            }
            return result;
        } catch (ServiceException e) {
            return new ResponseMessage(e);
        }
    }

    /**
     * 查询角色列表
     *
     * @param paging 分页信息
     * @param model 查询条件
     * @return 角色列表
     */
    @ResponseBody
    @RequestMapping("list")
    @RequiresPermissions("permission:role:query")
    public ResponseMessage list(RoleCoreWhere model, OrderPaging paging, String sceneType, Boolean extra,
            Boolean fillResources) {
        try {
            if (!SessionTools.isDevelopMode()) {
                model.setDefaults(false); // 只有开发模式才允许查询默认角色
            }
            PageList<RoleCoreBean> list = roleCoreQueryer.list(model, paging);
            ResponseMessage result = new ResponseMessage(list.toList());
            if (Boolean.TRUE.equals(fillResources)) {
                if (VerifyTools.isNotBlank(list)) {
                    List<String> roleIds = new ArrayList<>();
                    for (RoleCoreBean i : list) {
                        roleIds.add(i.getId());
                    }
                    RoleResourceRefWhere where = new RoleResourceRefWhere();
                    where.setRoleIds(roleIds);
                    where.setSceneType(sceneType);
                    PageList<RoleResourceRefBean> res = roleCoreQueryer.listRoleResource(where, OrderPaging.NONE);
                    result.addExtra("RoleResources", res.toList());
                }
            }
            return result;
        } catch (ServiceException e) {
            return new ResponseMessage(e);
        }
    }

    /** 创建角色 **/
    @ResponseBody
    @RequestMapping("create")
    @RequiresPermissions("permission:role:create")
    public ResponseMessage create(RoleCoreBean model, String sceneType, String[] resourceIds) {

        try {
            IAccount me = SessionTools.getLoginUser();
            roleCoreExecutor.create(model, me);

            // 创建角色权限关联
            if (VerifyTools.isNotBlank(resourceIds)) {
                List<String> ids = ConvertTools.toList(resourceIds);
                resourceCoreExecutor.distributeResources(model.getId(), sceneType, ids, me);
            }
            return new ResponseMessage(model);
        } catch (ServiceException e) {
            return new ResponseMessage(e);
        }
    }

    /** 修改角色 **/
    @ResponseBody
    @RequestMapping("update")
    @RequiresPermissions("permission:role:update")
    public ResponseMessage update(RoleCoreUpdate model, String sceneType, String[] resourceIds) {

        try {
            IAccount me = SessionTools.getLoginUser();
            roleCoreExecutor.update(model, me);

            // 修改角色权限关联
            if (resourceIds != null) { // resourceIds.size=0时清空所有权限 
                List<String> ids = ConvertTools.toList(resourceIds);
                resourceCoreExecutor.distributeResources(model.getId(), sceneType, ids, me);
            }
            return new ResponseMessage(model);
        } catch (ServiceException e) {
            return new ResponseMessage(e);
        }
    }

    /** 角色分配权限 **/
    @ResponseBody
    @RequestMapping("distribute")
    @RequiresPermissions("permission:role:distribute")
    public ResponseMessage distributeResources(String id, String sceneType, String[] resourceIds) {

        try {
            IAccount me = SessionTools.getLoginUser();
            List<String> list = ConvertTools.toListIfNullToEmpty(resourceIds);
            resourceCoreExecutor.distributeResources(id, sceneType, list, me);
            return new ResponseMessage();
        } catch (ServiceException e) {
            return new ResponseMessage(e);
        }
    }

    /** 删除角色 **/
    @ResponseBody
    @RequestMapping("delete")
    @RequiresPermissions("permission:role:delete")
    public ResponseMessage delete(String[] ids) {

        try {
            IAccount me = SessionTools.getLoginUser();
            roleCoreExecutor.delete(ConvertTools.toList(ids), me);
            return new ResponseMessage();
        } catch (ServiceException e) {
            return new ResponseMessage(e);
        }
    }

}
